class Admin::SessionsController < ApplicationController
  skip_before_action :verify_authenticity_token, only: [:login_admin_user]
  # 登陆页
  def index

  end
  # 注册页
  def new
  end

  # 注册创建
  def create
  end

  # 验证登陆页
  def login_admin_user
    msg = '登录成功'
    admin_users = AdminUser.where(sign_params)
    render json: {status: admin_user.present?, msg: "账号异常请联系管理员!"} and return if admin_users.count > 1
    admin_user = admin_users.last
    if admin_user.present?
      session[:admin_user_id] = admin_user.id
    else
      msg = '账号或密码错误'
    end
    render json: {status: admin_user.present?, msg: msg}
  end

  # 注销登陆
  def logout
    session[:admin_user_id] = nil if session[:admin_user_id].present?
    redirect_to admin_sessions_path, notice: "注销成功"
  end

  private
  def admin_user_params
    params.except(:authenticity_token, :commit).permit(:name, :account_number, :password, :password_confirmation)
  end

  def sign_params
    params.permit(:account_number, :password)
  end
end
